<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class user extends CI_Controller 
{
	protected $_templates;
	
	function __construct() 
	{
		parent::__construct();
		$this->load->model('user_model','user');
		$this->load->model('raovat_model','raovat');
		$this->account = get_ses_login() ;
		if(empty($this->account)) redirect('dangnhap');
	}
	
	public function index()
	{
		$this->profile() ;
	}
	
	public function profile()
	{
		$data['msg'] = '';
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('fullname','Họ tên','trim|required');
			$this->form_validation->set_rules('address','Địa chỉ','trim|required');
			$this->form_validation->set_rules('nb_phone','Điện thoại','trim|required');
			if($this->form_validation->run()== FALSE){
		 		$this->pre_message = validation_errors();
	  		}else{
				$req = $this->_build_form();
				if($this->user->save($req, $this->account['member_id']))
				{
					redirect('user');
					//$data['msg'] = 'Đã cập nhật';
				}
				
			}
		}
		$data['title'] = 'Thông tin thành viên' ;
		$data['username'] = $this->account['username'] ;
		$data['op_town'] = get_list_town() ;
		$data['rs'] = $this->user->get_id($this->account['member_id']) ;
		$data['page'] = 'user/account' ;
		$this->load->view('html/skin_raovat',$data) ;
	}
	
	function _build_form($flag=1)
	{
		$req["fullname"]= $this->input->post("fullname");
		$req["address"]= $this->input->post("address");
		$req["nb_phone"]= $this->input->post("nb_phone");
		$req["lb_yahoo"]= $this->input->post("lb_yahoo");
		$req["lb_skype"]= $this->input->post("lb_skype");
		$req["lb_fbook"] = $this->input->post("lb_fbook");
		$req["lb_website"] = $this->input->post("lb_website");
		$req["gender"]= $this->input->post("gender");
		$req["town_id"]= (int) $this->input->post("town_id");
		$req["lb_image"]= $this->input->post("lb_image");
		$req["user_ip"]= $this->input->ip_address();
		$req["dt_modify"]= date("Y-m-d H:i:s");
		if($flag) $req["dt_create"]= date("Y-m-d H:i:s");
		return $req;
	}
	
	public function tindadang()
	{
		$ses_login = get_ses_login() ;
		$data['title'] = "Tin đã đăng" ;
		//pagging
		$data['start'] = (int) $this->uri->segment(4) ; 
		$config['base_url'] = base_url().'raovat/tindadang/'.(int)$this->uri->segment(3);  
		$data['num'] = $this->raovat->count_all($ses_login['member_id']);
		$config['total_rows'] = $data['num'];
		$config['per_page'] = 25;
		$config['uri_segment'] = 4;
		$config['num_links'] = 3 ;
		$this->pagination->initialize($config);   
		$data['pagination'] = $this->pagination->create_links();
		//get data
		$data['list'] = $this->raovat->get_all($config['per_page'], (int) $this->uri->segment(4), $ses_login['member_id']);
		$data['page'] = 'tindadang' ;
		$this->load->view('html/skin_raovat',$data) ;
	}
	
	public function dangtin()
	{
		$data['title'] = "Đăng tin mới" ;
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('lb_subject','Chủ đề','trim|required');
			$this->form_validation->set_rules('lb_content','Nội dung','trim|required');
			$this->form_validation->set_rules('class_id','Ngành nghề','trim|required');
			if($this->form_validation->run()== FALSE){
		 		$this->pre_message = validation_errors();
	  		}else{
				$req = $this->_form_dangtin(1);
				if($this->raovat->save('raovat', $req))
				{
					redirect('user/tindadang');
				}
			}
		}
		$data['op_town'] = get_list_town() ;
		$data['op_class'] = get_list_class() ;
		
		$data['page'] = 'dangtin' ;
		$this->load->view('html/skin_raovat',$data) ;
	}
	
	public function editraovat($id)
	{
		$data['title'] = "Cập nhật tin" ;
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('lb_subject','Chủ đề','trim|required');
			$this->form_validation->set_rules('lb_content','Nội dung','trim|required');
			$this->form_validation->set_rules('class_id','Ngành nghề','trim|required');
			if($this->form_validation->run()== FALSE){
		 		$this->pre_message = validation_errors();
	  		}else{
				$req = $this->_form_dangtin(1);
				if($this->raovat->save('raovat', $req, 'raovat_id', $id))
				{
					redirect('user/tindadang');
				}
			}
		}
		$data['op_town'] = get_list_town() ;
		$data['op_class'] = get_list_class() ;
		
		$data['rs'] = $this->raovat->get_id($id) ;
		$data['page'] = 'editraovat' ;
		$this->load->view('html/skin_raovat',$data) ;
	}
	
	public function delete($id)
	{
		if($this->user->del('raovat', 'raovat_id', $id))
		{
			$this->session->set_flashdata('message','');
			redirect('user/tindadang') ;
		}
		else $this->session->set_flashdata('message','');
	}
	
	private function _form_dangtin($flag=0)
	{
		$req["lb_subject"]= $this->input->post("lb_subject");
		$req["lb_content"]= $this->input->post("lb_content");
		$req["class_id"]= $this->input->post("class_id");
		$req["town_id"]= $this->input->post("town_id");
		$req["member_id"]= $this->account["member_id"];
		$req["dt_modify"]= date("Y-m-d H:i:s");
		if($flag) $req["dt_create"]= date("Y-m-d H:i:s");
		$req["bl_active"] 	= (int)$this->input->post("bl_active");
		
		return $req ;
	}

	public function renew($id=0)
	{
		if($id)
		{
			$data['dt_modify'] = date('Y-m-d H:i:s');
			$this->db->where('raovat_id', $id) ;
			if($this->db->update('raovat', $data))
			{
				$res['code'] = 1 ;
				$res['date'] = date('d.m.y') ;
				echo json_encode($res) ;
				return ;
			}
		}
		$res['code'] = 0 ;
		$res['date'] = date('d.m.y') ;
		echo json_encode($res) ;
		return ;
	}
}

?>